﻿using Application.Services.Dto;
using Application.Services.Dto.Organization.Request;
using Application.Services.Interface.Organization;
using Domain.Interface.Organization;
using Framework.Domain.Interface.Factorys;
using Repository.Interface.Organization;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Linq.Expressions;
using Repository.Interface.SearchCriterias;
using Application.Services.Dto.Organization.Department;

namespace Application.Services.Organization
{
    public class DepartmentAppServices : BaseAppServices, IDepartmentAppService
    {

        #region 部门

        #region 创建部门
        /// <summary>
        /// 创建部门
        /// </summary>
        /// <param name="deptName"></param>
        /// <returns></returns>
        public BaseResponseDto<bool> CrreateDepartment(CreateDepartmentDto req)
        {
            BaseResponseDto<bool> resp = new BaseResponseDto<bool>();

            string maxDeptNo = "00";
            IDepartmentDomain baseDeptDomain = null;
            IDepartmentDomain deptDomain = base.BuildDepartmentDomain(req.DeptName);
            deptDomain.SetIsDelete(true);
            int level = 1;
            if (req.ParentKeyId.HasValue)
            {
                baseDeptDomain = base.DepartmentRepository.Find(req.ParentKeyId.Value);
                if (baseDeptDomain == null)
                {
                    return new BaseResponseDto<bool>()
                    {
                        Result = false,
                        Error = "指定的父级部门不存在"
                    };
                }
                IDepartmentRepository deptRepository = ExampleFactory.CreateAppService<IDepartmentRepository>();
                var respList = deptRepository.GetQuery(new DepartmentSearchCriteria()
                {
                    ParentKeyId = req.ParentKeyId.Value
                }).ToList();
                if (respList != null && respList.Count > 0)
                {
                    maxDeptNo = respList.Select(i => i.GetDepartmentNo()).Max();
                }
                deptDomain.SetParentKeyId(req.ParentKeyId.Value);
                if (maxDeptNo != "00")
                {
                    int maxDeptNoLength = maxDeptNo.Length;
                    maxDeptNo = (Convert.ToInt32(maxDeptNo) + 1).ToString().PadLeft(maxDeptNoLength, '0');
                }
                else
                {
                    maxDeptNo = baseDeptDomain?.GetDepartmentNo() + (Convert.ToInt32(maxDeptNo) + 1).ToString().PadLeft(2, '0');
                }
                level = baseDeptDomain != null ? baseDeptDomain.GetLevel() + 1 : -1;
            }
            else
            {
                IDepartmentRepository deptRepository = ExampleFactory.CreateAppService<IDepartmentRepository>();
                maxDeptNo = deptRepository.GetQuery(new DepartmentSearchCriteria()
                {
                    Level = 1
                }).Select(i => i.GetDepartmentNo()).Max();
                maxDeptNo = (Convert.ToInt32(maxDeptNo) + 1).ToString().PadLeft(2, '0');
            }
            deptDomain.SetDepartmentNo(maxDeptNo);
            deptDomain.SetLevel(level);
            if (req.DeptChiefKeyId.HasValue)
            {
                deptDomain.SetDeptChiefKeyId(req.DeptChiefKeyId.Value);
            }

            base.DepartmentRepository.Add(deptDomain);

            resp.Result = base.SaveChanges() > 0;

            return resp;
        }
        #endregion

        public BaseResponseDto<List<DepartmentResponseDto>> GetDepartmentAll()
        {
            BaseResponseDto<List<DepartmentResponseDto>> resp = new BaseResponseDto<List<DepartmentResponseDto>>();



            return resp;
        }



        public BaseResponseDto<DepartmentResponseDto> GetDepartmenByKeyId(Guid keyId)
        {
            BaseResponseDto<DepartmentResponseDto> resp = new BaseResponseDto<DepartmentResponseDto>();
            
            try
            {

                IDepartmentDomain deptDomain = base.DepartmentRepository.Find(keyId);
                if (deptDomain != null)
                {
                    resp.Result = new DepartmentResponseDto()
                    {
                        DepartmentName = deptDomain.GetDepartmentName(),
                        DepartmentNo = deptDomain.GetDepartmentNo(),
                        KeyId = deptDomain.GetKeyId(),
                        Level = deptDomain.GetLevel(),
                        ParentKeyId = deptDomain.GetParentKeyId()
                    };
                }

            }
            catch (Exception ex)
            {
                resp.Flag = false;
                resp.Error = ex.Message.ToString();
            }

            return resp;
        }
        #endregion



    }
}
